﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace IBLL
{
    public interface IBaseService<TEntity>
    {
        #region 添加
        /// <summary>
        /// 实现对数据的添加功能
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        int AddEntity(TEntity entity);
        Task<int> AddEntityAsync(TEntity entity);
        #endregion

        #region 修改
        /// <summary>
        /// 实现对数据的修改功能
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>实体</returns>
        int ModifyEntity(TEntity entity);
        Task<int> ModifyEntityAsync(TEntity entity);
        #endregion

        #region 删除（伪删除）
        /// <summary>
        /// 实现对数据的删除功能,用修改的方法实现伪删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns>被“删除”的实体</returns>
        bool DeleteEntityById(int id);
        Task<bool> DeleteEntityByIdAsync(int id);

        /// <summary>
        /// 根据条件对数据批量删除,用修改的方法实现伪删除
        /// </summary>
        /// <param name="whereLambda">条件表达式</param>
        bool DeleteEntitysByLambda(Expression<Func<TEntity, bool>> whereLambda);
        Task<bool> DeleteEntitysByLambdaAsync(Expression<Func<TEntity, bool>> whereLambda);
        #endregion

        #region 查询
        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <returns></returns>
        TEntity GetEntityById(int id);
        Task<TEntity> GetEntityByIdAsync(int id);

        /// <summary>
        /// 获取所有实体，排序字段为'Id'(实体基类中含有Id字段)
        ///对于大数据表，慎用，IEnumerable比List加载快，边显示边查，list要全部查完才显示
        /// </summary>        
        IEnumerable<TEntity> GetAllEntitysList();        

        /// <summary>
        /// 根据条件获取一定数量的实体集合，慎用，排序字段和顺序默认
        /// </summary> 
        IEnumerable<TEntity> GetAnyEntitysList(int count, Expression<Func<TEntity, bool>> whereLambda);

        /// <summary>
        /// 根据条件获取实体，排序字段和顺序默认       
        /// </summary>
        /// <param name="whereLambda">查询条件</param>
        /// <returns>实体的IQueryable语句</returns>
        IQueryable<TEntity> GetEntitysByLambda(Expression<Func<TEntity, bool>> whereLambda);

        /// <summary>
        ///实现数据的查询并排序
        /// </summary>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderByLambda">排序字段，数据库中必须存在，不能为空，区分大小写</param>
        /// <param name="isAsc">如何排序，根据倒叙还是升序,true升序</param>
        /// <returns></returns>
        IQueryable<TEntity> GetEntitysByLambdaAndSort(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, object>> orderLambda, bool isAsc);
        #endregion

        #region 分页查询       

        /// <summary>
        /// 实现对数据的分页查询
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">一页显示多少条数据</param>
        /// <param name="rowCount">总条数</param> 
        /// <param name="pageCount">总页数</param> 
        /// <param name="whereLambda">条件表达式</param>
        /// <param name="isAsc">如何排序，根据倒叙还是升序</param>
        /// <param name="orderByLambda">排序字段</param>
        /// <returns></returns>
        List<TEntity> GetPagedEntitysByLambdaAndSort(int pageIndex, int pageSize, out int rowCount, out int pageCount, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, object>> orderLambda, bool isAsc);

        #endregion
    }
}
